#include"include/lyricpanel.h"
#include"include/lyricsetting.h"

LyricPanel::LyricPanel(QWidget *parent):QWidget(parent)
{
    setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
    QPalette pal = palette();
    LyricSetting settings;
    pal.setColor(QPalette::Background, settings.getDesktopLyricBackColor());
    setPalette(pal);
    setAttribute(Qt::WA_TranslucentBackground);

    QSettings sets("HJF","Minip");
    locked=sets.value("DesktopLyricLocked",false).toBool();
    text="Minip,Mini Music Player...";
    QFont f=settings.getDesktopLyricFont();
    setTextFont(f);
    setColor(settings.getDesktopLyricColor());
    int height=f.pointSize()+100;
    int width=QApplication::desktop()->width()-300;
    resize(width,height);
    move(150,QApplication::desktop()->height()-height);
}

void LyricPanel::setColor(const QColor &fC)
{
    foreColor=fC;
}

void LyricPanel::setText(const QString &str)
{
    text=str;
}

void LyricPanel::setTextFont(const QFont &f)
{
    font=f;
}

void LyricPanel::mousePressEvent(QMouseEvent *me)
{
    if(!locked)
    {
        clickPos=me->pos();
        startPos=this->pos();
    }
}

void LyricPanel::mouseMoveEvent(QMouseEvent *me)
{
    if(!locked)
    {
        move(startPos+(me->pos()-clickPos));
    }
}

void LyricPanel::paintEvent(QPaintEvent *pe)
{
    rect=QRectF(0,0,width(),height());
    QPainter painter(this);
    //    painter.setBackgroundMode(Qt::OpaqueMode);
    //    QBrush brush;
    //    brush.setColor(backColor);
    //    painter.setBackground(brush);
    painter.setFont(font);
    //    painter.drawRect(rect);
    //    painter.fillRect(rect,backColor);
    painter.setPen(foreColor);
    painter.drawText(rect,text);
}

void LyricPanel::closeEvent(QCloseEvent *ce)
{
    if(!locked)
        emit needed(false);
    ce->setAccepted(!locked);
}

void LyricPanel::setLocked(bool b)
{
    locked=b;
    QSettings sets("HJF","Minip");
    sets.setValue("DesktopLyricLocked",locked);
    sets.sync();
}

bool LyricPanel::isLocked()
{
    return locked;
}
